home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / LISTBOX / CHECKLB / CHECKLB.ZIP / CheckLB.pas < prev    next >
Pascal/Delphi Source File  |  1996-08-30  |  11KB  |  318 lines

  1. { -------------------------------------------------------------------------------------}
  2. { A "CheckListBox" component for Delphi32.                                             }
  3. { Copyright 1996, Patrick Brisacier.  All Rights Reserved.                             }
  4. { This component can be freely used and distributed in commercial and private          }
  5. { environments, provided this notice is not modified in any way.                       }
  6. { -------------------------------------------------------------------------------------}
  7. { Feel free to contact us if you have any questions, comments or suggestions at        }
  8. { PBrisacier@mail.dotcom.fr (Patrick Brisacier)                                        }
  9. { -------------------------------------------------------------------------------------}
  10. { Date last modified:  08/15/96                                                        }
  11. { -------------------------------------------------------------------------------------}
  12.  
  13. { -------------------------------------------------------------------------------------}
  14. { TCheckListBox v1.00                                                                  }
  15. { -------------------------------------------------------------------------------------}
  16. { Description:                                                                         }
  17. {   A component that adds check property to ListBoxes items.                           }
  18. { Added Properties to ListBox:                                                         }
  19. {   property Checked[Index: Integer]: Boolean;          "Run-time only property"       }
  20. {   property State[Index: Integer]: TCheckBoxState;     "Run-time only property"       }
  21. {   property AllowGrayed: Boolean;                                                     }
  22. {   property Offset: Integer;                                                          }
  23. {                                                                                      }
  24. { See example contained in example.zip file for more details.                          }
  25. { -------------------------------------------------------------------------------------}
  26. { Revision History:                                                                    }
  27. { 1.00:  + Initial release                                                             }
  28. { -------------------------------------------------------------------------------------}
  29.  
  30. unit CheckLB;
  31.  
  32. interface
  33.  
  34. uses
  35.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  36.   StdCtrls;
  37.  
  38. type
  39.   TItemState = class
  40.   public
  41.     Checked: Boolean;
  42.     State: TCheckBoxState;
  43.   end;
  44.  
  45.   TCheckListBox = class(TCustomListBox)
  46.   private
  47.     { DΘclarations privΘes }
  48.     { for the bitmaps }
  49.     FBmpChecked: TBitmap;
  50.     FBmpGrayed: TBitmap;
  51.     FBmpUnchecked: TBitmap;
  52.     { for the state of every elements }
  53.     FAllowGrayed: Boolean;
  54.     { Offset between the listbox border and the bitmap }
  55.     { and between the bitmap and the text              }
  56.     FOffset: Integer;
  57.  
  58.     { Create an object associated with an item if it doesn't exist }
  59.     procedure CreateObject(Index: Integer);
  60.  
  61.     procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
  62.   protected
  63.     { Set and Get functions for the properies }
  64.     procedure SetChecked(Index: Integer; const AChecked: Boolean);
  65.     function GetChecked(Index: Integer): Boolean;
  66.     procedure SetState(Index: Integer; const AState: TCheckBoxState);
  67.     function GetState(Index: Integer): TCheckBoxState;
  68.     procedure SetOffset(AnOffset: Integer);
  69.  
  70.     procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override;
  71.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  72.   public
  73.     { constructor and destructor }
  74.     constructor Create(AOwner: TComponent); override;
  75.     destructor Destroy; override;
  76.     { new properties for the checkbox management }
  77.     property Checked[Index: Integer]: Boolean
  78.              read GetChecked write SetChecked;
  79.     property State[Index: Integer]: TCheckBoxState
  80.              read GetState write SetState;
  81.   published
  82.     { new properties for the checkbox management }
  83.     property AllowGrayed: Boolean
  84.              read FAllowGrayed write FAllowGrayed;
  85.     property Offset: Integer
  86.              read FOffset write SetOffset
  87.              default 4;
  88.  
  89.     { pusblish the TListBox properties }
  90.     property Align;
  91.     property BorderStyle;
  92.     property Color;
  93.     property Columns;
  94.     property Ctl3D;
  95.     property DragCursor;
  96.     property DragMode;
  97.     property Enabled;
  98.     property ExtendedSelect;
  99.     property Font;
  100.     property IntegralHeight;
  101.     property ItemHeight;
  102.     property Items;
  103.     property MultiSelect;
  104.     property ParentColor;
  105.     property ParentCtl3D;
  106.     property ParentFont;
  107.     property ParentShowHint;
  108.     property PopupMenu;
  109.     property ShowHint;
  110.     property Sorted;
  111.     property Style default lbOwnerDrawFixed;
  112.     property TabOrder;
  113.     property TabWidth;
  114.     property Visible;
  115.  
  116.     { pusblish the TListBox events }
  117.     property OnClick;
  118.     property OnDblClick;
  119.     property OnDragDrop;
  120.     property OnDragOver;
  121.     property OnDrawItem;
  122.     property OnEndDrag;
  123.     property OnEnter;
  124.     property OnExit;
  125.     property OnKeyDown;
  126.     property OnKeyPress;
  127.     property OnKeyUp;
  128.     property OnMeasureItem;
  129.     property OnMouseDown;
  130.     property OnMouseMove;
  131.     property OnMouseUp;
  132.     property OnStartDrag;
  133.  
  134.   end;
  135.  
  136. procedure Register;
  137.  
  138. implementation
  139.  
  140. { include the resource file which contains the bitmaps }
  141. {$R CheckLB.res }
  142.  
  143. constructor TCheckListBox.Create(AOwner: TComponent);
  144. begin
  145.   inherited Create(AOwner);
  146.   { load the bitmaps from the resource file }
  147.   FBmpChecked := TBitmap.Create;
  148.   FBmpChecked.LoadFromResourceName(hInstance, 'CHECKED');
  149.   FBmpGrayed := TBitmap.Create;
  150.   FBmpGrayed.LoadFromResourceName(hInstance, 'GRAYED');
  151.   FBmpUnchecked := TBitmap.Create;
  152.   FBmpUnchecked.LoadFromResourceName(hInstance, 'UNCHECKED');
  153.   { initialize the default values }
  154.   FOffset := 4;
  155.   Style := lbOwnerDrawFixed;
  156. end;
  157.  
  158. destructor TCheckListBox.Destroy;
  159. begin
  160.   { free the bitmaps }
  161.   FBmpChecked.Free;
  162.   FBmpGrayed.Free;
  163.   FBmpUnchecked.Free;
  164.   inherited Destroy;
  165. end;
  166.  
  167. procedure TCheckListBox.CreateObject(Index: Integer);
  168. var
  169.   MyItemState: TItemState;
  170. begin
  171.   { check the range of Index }
  172.   if (Index < 0) or (Index >= Items.Count) then Exit;
  173.   if Items.Objects[Index] = nil then begin
  174.     MyItemState := TItemState.Create;
  175.     Items.Objects[Index] := MyItemState;
  176.   end;
  177. end;
  178.  
  179. procedure TCheckListBox.SetChecked(Index: Integer; const AChecked: Boolean);
  180. begin
  181.   { check the range of Index }
  182.   if (Index < 0) or (Index >= Items.Count) then Exit;
  183.   CreateObject(Index);
  184.   if TItemState(Items.Objects[Index]).Checked <> AChecked then begin
  185.     TItemState(Items.Objects[Index]).Checked := AChecked;
  186.     if AChecked then
  187.       TItemState(Items.Objects[Index]).State := cbChecked
  188.     else
  189.       TItemState(Items.Objects[Index]).State := cbUnchecked;
  190.     Invalidate;
  191.   end;
  192. end;
  193.  
  194. function TCheckListBox.GetChecked(Index: Integer): Boolean;
  195. begin
  196.   { check the range of Index }
  197.   if (Index < 0) or (Index >= Items.Count) then Exit;
  198.   CreateObject(Index);
  199.   Result := TItemState(Items.Objects[Index]).Checked;
  200. end;
  201.  
  202. procedure TCheckListBox.SetState(Index: Integer; const AState: TCheckBoxState);
  203. begin
  204.   { check the range of Index }
  205.   if (Index < 0) or (Index >= Items.Count) then Exit;
  206.   CreateObject(Index);
  207.   if TItemState(Items.Objects[Index]).State <> AState then begin
  208.     TItemState(Items.Objects[Index]).State := AState;
  209.     case AState of
  210.     cbChecked:
  211.       TItemState(Items.Objects[Index]).Checked := True;
  212.     cbGrayed:
  213.       TItemState(Items.Objects[Index]).Checked := False;
  214.     cbUnchecked:
  215.       TItemState(Items.Objects[Index]).Checked := False;
  216.     end;
  217.     Invalidate;
  218.   end;
  219. end;
  220.  
  221. function TCheckListBox.GetState(Index: Integer): TCheckBoxState;
  222. begin
  223.   { check the range of Index }
  224.   if (Index < 0) or (Index >= Items.Count) then Exit;
  225.   CreateObject(Index);
  226.   Result := TItemState(Items.Objects[Index]).State;
  227. end;
  228.  
  229. procedure TCheckListBox.SetOffset(AnOffset: Integer);
  230. begin
  231.   if FOffset <> AnOffset then begin
  232.     FOffset := AnOffset;
  233.     Invalidate;
  234.   end;
  235. end;
  236.  
  237. procedure TCheckListBox.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState);
  238. var
  239.   TheBitmap: TBitmap;
  240.   X, Y: Integer;
  241. begin
  242.   if Assigned(OnDrawItem) then
  243.     OnDrawItem(Self, Index, Rect, State)
  244.   else begin
  245.     { calculate the right bitmap to use }
  246.     case Self.State[Index] of
  247.     cbChecked:
  248.       TheBitmap := FBmpChecked;
  249.     cbGrayed:
  250.       TheBitmap := FBmpGrayed;
  251.     cbUnchecked:
  252.       TheBitmap := FBmpUnchecked;
  253.     end;
  254.     { draw the item }
  255.     with Canvas do begin
  256.       FillRect(Rect);
  257.       X := Rect.Left + FOffset;
  258.       Y := Rect.Top + (Rect.Bottom - Rect.Top - TheBitmap.Height) div 2;
  259.       Draw(X, Y, TheBitmap);
  260.       X := X + TheBitmap.Width + FOffset;
  261.       Y := Rect.Top + (Rect.Bottom - Rect.Top - TextHeight(' ')) div 2;
  262.       Rect.Left := X;
  263.       TextRect(Rect, X, Y, Items[Index]);
  264.     end;
  265.   end;
  266. end;
  267.  
  268. procedure TCheckListBox.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  269. var
  270.   Index: Integer;
  271.   Rect: TRect;
  272. begin
  273.   Index := ItemAtPos(Point(X, Y), True);
  274.   if Index <> -1 then begin
  275.     Rect := ItemRect(Index);
  276.     if (Button = mbLeft)
  277.         and (X >= Rect.Left + FOffset)
  278.         and (X < Rect.Left + FOffset + FBmpChecked.Width) then
  279.     begin
  280.       if FAllowGrayed then begin
  281.         case State[Index] of
  282.         cbChecked:
  283.           State[Index] := cbUnchecked;
  284.         cbGrayed:
  285.           State[Index] := cbChecked;
  286.         cbUnchecked:
  287.           State[Index] := cbGrayed;
  288.         end;
  289.       end
  290.       else
  291.         Checked[Index] := not Checked[Index];
  292.       Invalidate;
  293.     end;
  294.   end;
  295.   inherited;
  296. end;
  297.  
  298. procedure TCheckListBox.CMFontChanged(var Message: TMessage);
  299. var
  300.   BitmapHeight, FontHeight: Integer;
  301. begin
  302.   inherited;
  303.   Canvas.Font := Font;
  304.   BitmapHeight := FBmpChecked.Height;
  305.   FontHeight := Canvas.TextHeight(' ');
  306.   if FontHeight > BitmapHeight then
  307.     ItemHeight := FontHeight
  308.   else
  309.     ItemHeight := BitmapHeight;
  310. end;
  311.  
  312. procedure Register;
  313. begin
  314.   RegisterComponents('SystΦme', [TCheckListBox]);
  315. end;
  316.  
  317. end.
  318.